﻿Imports MeoTom.Core

Namespace Control
    Partial Public Class LinkEdit
        Inherits UserControl
        Public Lc As New BLL.LinkCat()
        Public L As New BLL.Link()
        Public Util As New Utilities.Common()

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            If Not IsPostBack Then
                Dim previousPage As String = ""
                If Request.QueryString.ToString = vbNullString Then
                    previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO"))
                Else
                    previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO") & "?" & Request.QueryString.ToString())
                End If
                Util.ValidateLogin(previousPage)

                L.LinkID = Integer.Parse(Request.QueryString("ID"))
                L.GetByID()

                txtLink.Text = L.LinkAddress
                txtName.Text = L.LinkName
                txtDesc.Text = L.LinkDesc
                divTargetType.InnerHtml = WriteTarget(L.TargetType.ToString)
                chkIsPrivate.Checked = L.IsPrivate
                catdetail.InnerHtml = Lc.WriteLinkCat(3, L.GetAllCat(1))
            End If
        End Sub

        Private Function WriteTarget(ByVal targetType As String) As String
            Dim strOut As New StringBuilder()
            Dim strWriteChecked0 As String = " checked=""checked"""
            Dim strWriteChecked1 As String = " checked=""checked"""
            Dim strWriteChecked2 As String = " checked=""checked"""
            If targetType = "0" Then
                strWriteChecked1 = ""
                strWriteChecked2 = ""
            ElseIf targetType = "1" Then
                strWriteChecked0 = ""
                strWriteChecked2 = ""
            ElseIf targetType = "2" Then
                strWriteChecked1 = ""
                strWriteChecked0 = ""
            End If

            strOut.Append("<p><label><input type=""radio""").Append(strWriteChecked0).Append(" style=""width:1%;"" name=""cboTarget"" value=""0"" /><span style=""background-color:#DFDFDF;"">_blank</span> - new window or tab.</label>")
            strOut.Append("<label><input type=""radio""").Append(strWriteChecked1).Append(" style=""width:1%;"" name=""cboTarget"" value=""1"" /><span style=""background-color:#DFDFDF;"">_top</span> - current window or tab, with no frames.</label>")
            strOut.Append("<label><input type=""radio""").Append(strWriteChecked2).Append(" style=""width:1%;"" name=""cboTarget"" value=""2"" /><span style=""background-color:#DFDFDF;"">_none</span> - same window or tab.</label></p>")
            strOut.Append("<p>Choose the target frame for your link.</p>")
            Return strOut.ToString()
        End Function

        Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
            l.LinkID = Integer.Parse(Request.QueryString("ID"))
            l.IsPrivate = chkIsPrivate.Checked
            l.LinkAddress = util.NoHTML(txtLink.Text)
            l.LinkDesc = util.NoHTML(txtDesc.Text)
            l.LinkName = util.NoHTML(txtName.Text)
            l.TargetType = Integer.Parse(Request.Form("cboTarget"))
            l.Update()
            l.LinkID = Integer.Parse(Request.QueryString("ID"))
            l.RemoveTempLinkCat()
            If Request.Form.Get("cboCategory") = vbNullString Then
                l.LinkCat = 1
                l.SetTempLinkCat(1)
            Else
                For Each strCatID As String In Regex.Split(Request.Form("cboCategory").ToString(), ",")
                    l.LinkCat = Integer.Parse(strCatID)
                    l.SetTempLinkCat(1)
                Next
            End If
            lc.WriteLinkCat(5)
            Response.Redirect("/mt-inside/LinkManager.aspx")
        End Sub

        Private Sub lnkDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkDelete.Click
            l.LinkID = Integer.Parse(Request.QueryString("ID"))
            l.Delete()
            lc.WriteLinkCat(5)
            Response.Redirect("/mt-inside/LinkManager.aspx")
        End Sub
    End Class
End Namespace